<template>
  <div>
    <el-table
      :data="seriesList"
      style="width: 100%"
    >
      <el-table-column
        :label="$t('series')"
        prop="name"
      />
      <el-table-column
        :label="$t('id')"
        prop="idCode"
      />
      <el-table-column
        align="right"
      >
        <template slot="header" slot-scope="scope">
          <el-button
            icon="el-icon-plus"
            type="text"
            @click="showAdd"
          />
        </template>
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            type="text"
            @click="updateOtaSeries( scope.row)"
          />
          <el-button
            icon="el-icon-delete"
            type="text"
            style="color: red"
            @click="deleteOtaSeries( scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogSeriesFormVisible"
    >
      <el-form
        label-width="120px"
        :model="series"
      >
        <el-form-item :label="$t('name')">
          <el-input v-model="series.name" />
        </el-form-item>
        <el-form-item :label="$t('keyName')">
          <el-input v-model="series.idCode" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSeriesFormVisible = false">{{ $t('cancel') }}</el-button>
        <el-button type="primary" @click="addOrUpdateSeries">{{ $t('confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  deleteOtaSeries,
  insertOtaSeries,
  updateOtaSeries,
  listOtaSeries
} from '@/api/ota/otaSeriesApi'

export default {
  name: 'Series',
  props: {
    productId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      seriesList: [],
      dialogSeriesFormVisible: false,
      series: {},
      seriesModel: 'insert'
    }
  },
  watch: {
    productId: {
      deep: true,
      handler(val) {
        if (this.productId) {
          this.listOtaSeries()
        }
      }
    }
  },
  created() {
    if (this.productId) {
      this.listOtaSeries()
    }
  }, methods: {
    deleteOtaSeries(item) {
      const that = this
      this.$confirm(this.$t('deletePrompt'), this.$t('prompt'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      })
        .then(function() {
          deleteOtaSeries({
            seriesId: item.seriesId
          }).then(res => {
            const { code } = res
            if (code === '200') {
              that.listOtaSeries()
            }
          })
        })
    },
    updateOtaSeries(item) {
      this.series = item
      this.seriesModel = 'update'
      this.dialogSeriesFormVisible = true
    },
    showAdd() {
      this.series = {
        productId: this.productId
      }
      this.seriesModel = 'insert'
      this.dialogSeriesFormVisible = true
    },
    addOrUpdateSeries() {
      switch (this.seriesModel) {
        case 'insert':
          insertOtaSeries({
            ...this.series
          }).then(res => {
            const { code } = res
            if (code === '200') {
              this.listOtaSeries()
            }
            this.dialogSeriesFormVisible = false
          })
          break
        case 'update':
          updateOtaSeries({
            ...this.series
          }).then(res => {
            const { code } = res
            if (code === '200') {
              this.listOtaSeries()
            }
            this.dialogSeriesFormVisible = false
          })
          break
      }
    },
    listOtaSeries() {
      listOtaSeries({
        productId: this.productId
      }).then(res => {
        const { code, data } = res
        if (code === '200') {
          this.seriesList = data
          this.series = data[0]
        } else {
          this.seriesList = []
          this.series = {}
        }
      })
    }
  }
}
</script>
<style scoped>
  .item{
    width: 100%;
    background: #cfcfcf;
    border-radius: 13px;
    height: 40px;
    padding: 5px;
    margin: 5px;
    display: flex;
    justify-content: space-between;
  }
</style>
